<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link type="text/css" rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
</head>
<body>
<div id="vuebox">
    <div class="container" style="margin-top: 50px">
        <div class="row">
            <div class="col-md-6">
            </div>
            <div class="col-md-4 text-right">
                <a href="#" class="btn btn-primary" @click.stop="showModal()">新增</a>
            </div>
        </div>
        <br/>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>手机</th>
                <th>QQ</th>
                <th>Email</th>
                <th>地址</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="item in entries.data.list">
                <td>{{item.id}}</td>
                <td>{{item.cname}}</td>
                <td>{{item.mobile}}</td>
                <td>{{item.qq}}</td>
                <td>{{item.email}}</td>
                <td>{{item.address}}</td>
                <td>
                    <button class="btn btn-group" @click="to_update_contact(item.id)">修改</button>
                    <button class="btn btn-success"@click="deleteContact(item.id)">删除</button>
                </td>
            </tr>
            </tbody>
        </table>
        <br/>
        <div class="text-center">
            <ul class="pagination">
                <li :class="{disabled:!entries.data.hasPreviousPage}">
                    <a href="#" @click.stop="selectContact(entries.data.perPage)">&laquo;</a>
                </li>
                <li :class="{active:entries.data.pageNum==i}" v-for="i in entries.data.navigatepageNums">
                    <a href="#" @click.stop="selectContact(i)">{{i}}</a>
                </li>
                <li :class="{disabled:!entries.data.hasNextPage}">
                    <a href="#" @click.stop="selectContact(entries.data.nextPage)">&raquo;</a>
                </li>
            </ul>
        </div>

        <!--模态框-->
        <div class="modal" id="mymodal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" type="button" @click="hideModal()">&times;</button>
                        <div class="modal-title">
                            添加
                        </div>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label for="id" class="col-sm-2 control-label">编号</label>
                                <div class="col-sm-10">
                                    <input v-model="new_contact.id" id="id" type="text" class="form-control"  placeholder="自增">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="cname" class="col-sm-2 control-label">姓名</label>
                                <div class="col-sm-10">
                                    <input v-model="new_contact.cname" id="cname" type="text" class="form-control" placeholder="请输入姓名">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="mobile" class="col-sm-2 control-label">电话</label>
                                <div class="col-sm-10">
                                    <input v-model="new_contact.mobile" id="mobile" type="text" class="form-control" placeholder="请输入电话"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="qq" class="col-sm-2 control-label">QQ</label>
                                <div class="col-sm-10">
                                    <input v-model="new_contact.qq" id="qq" type="text" class="form-control" placeholder="请输入QQ">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="email" class="col-sm-2 control-label">Email</label>
                                <div class="col-sm-10">
                                    <input v-model="new_contact.email" id="email" type="text" class="form-control" placeholder="请输入Email">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="address" class="col-sm-2 control-label">地址</label>
                                <div class="col-sm-10">
                                    <input v-model="new_contact.address" id="address" type="text" class="form-control" placeholder="请输入地址">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-default" @click="hideModal()">关闭</button>
                        <button class="btn btn-primary" @click="saveContact()">保存</button>
                    </div>
                </div>
            </div>
        </div>


    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var vm = new Vue({
        el:"#vuebox",
        data:{
            entries:{},
            pageNum:'',
            delResult:{},
            new_contact:{id:'', cname:'',mobile:'',qq:'',email:'',address:''}
        },
        methods:{
            selectContact:function (pageNum) {
                axios.get("/selectContact",{params:{pageNum:pageNum}}).then(res=>{
                    this.entries=res.data;
                    this.pageNum=pageNum;
                });
            },
            deleteContact:function (id) {
                axios.get("/delContactById",{params:{id:id}}).then(res=>{

                    this.delResult=res.data;
                    if (this.delResult.result=='SUCCESS'){
                        alert("删除成功")
                        this.selectContact(this.pageNum);
                    }else {
                        alert(this.delResult.message);
                    }
                })
            },
            showModal:function () {
                $('#mymodal').modal('show');
            },
            hideModal:function () {
                $('#mymodal').modal('hide');
                this.clearForm();

            },
            clearForm:function () {
                $('#id').val();
                $('#cname').val();
                $('#mobile').val();
                $('#qq').val();
                $('#email').val();
                $('#address').val();
            },
            saveContact:function () {
                axios.post("/saveContact",this.new_contact).then(res=>{
                    this.delResult=res.data;
                    if (this.delResult.result=='SUCCESS'){
                        this.selectContact(this.pageNum);
                        this.hideModal();
                    }else {
                        alert(this.delResult.message);
                    }
                })
            },
            to_update_contact:function(id){
                axios.get("/getContactById",{params:{id:id}}).then(res=>{
                    this.new_contact=res.data.data;
                    this.showModal();
                })
            }
        }
    });
</script>